﻿//  --------------------------------
//  Copyright (c) 2010 Michael Schuler, Sascha Burger. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://p2pmessaging.codeplex.com/license
//  ---------------------------------
using System;
using System.Collections.Generic;
using System.Threading;
using NUnit.Framework;
using p2pMessaging.BL.Model;
using p2pMessaging.BL.Plugin;
using p2pMessaging.Contracts;
using p2pMessaging.Interfaces;

namespace p2pMessaging.Tests
{
	[TestFixture]
	public class P2PMessageTest
	{
		private IMessageService myMessageService;
		
		[SetUp]
		public void setUp()
		{
			myMessageService = ServiceLocator.Get<IMessageService>("P2P");
		}
		
		[Test]
		public void Send()
		{
			Message msg = new Message();
			msg.Data = "Hallo Welt";
			msg.Recipient.Add(new Contact { IpAddress = "127.0.0.1" });
			msg.Subject = "Titel";

			Assert.IsTrue(((P2PMessageService) myMessageService).IsThreadRunning);
			myMessageService.Send(msg);
			Thread.Sleep(500);

			Assert.AreEqual(myMessageService.PluginId, msg.PluginId);

			List<IMessage> result = myMessageService.Receive();

			Assert.AreEqual(1, result.Count);

			Assert.AreEqual(myMessageService.PluginId, result[0].PluginId);
			Assert.AreEqual(msg.Data, result[0].Data);
			Assert.AreEqual(msg.Subject, result[0].Subject);
			
			myMessageService.Dispose();
			Assert.IsFalse(((P2PMessageService)myMessageService).IsThreadRunning);
		}

		[TearDown]
		public void TearDown()
		{
			myMessageService.Dispose();
		}
		
		[Test]
		[ExpectedException(ExceptionType = typeof(ArgumentNullException))]
		public void SendNull()
		{
			myMessageService.Send(null);
		}
		
		[Test]
		public void GetGuid()
		{
			Assert.AreNotEqual(Guid.Empty, myMessageService.PluginId);
		}
	}
}
